package com.qo.qsdk.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.messaging.FirebaseMessaging;
import org.jetbrains.annotations.NotNull;

public class FirebaseManager {

    private static final String TAG = FirebaseManager.class.getSimpleName();

    private static class FirebaseManagerHolder {
        private static final FirebaseManager instance_ = new FirebaseManager();
    }

    public static FirebaseManager getInstance() {
        return FirebaseManagerHolder.instance_;
    }

    private boolean open() {
        return true;
    }

    public FirebaseManager() {

    }

    public void init(final Context activity) {
        if (open()) {
            FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
                @Override
                public void onComplete(@NonNull @NotNull Task<String> task) {
                    if (!task.isSuccessful()) {
                        Log.w(TAG, "GetInstanceId failed: ", task.getException());
                        return;
                    }
//                    if (activity != null) {
//                        QSDKHttp.getInstance().firebaseToken(activity, task.getResult(), QSDKHelper.getInstance().getUserInfoModel(activity).getToken(), new QResultCallback<QCommonResultBean<String>>() {
//                            @Override
//                            public void onSuccess(QCommonResultBean<String> response) {
//
//                            }
//
//                            @Override
//                            public void onError(int code, String msg) {
//
//                            }
//                        });
//                    }
                }
            });

            FirebaseMessaging.getInstance().subscribeToTopic(activity.getPackageName()).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull @NotNull Task<Void> task) {
                    if (!task.isSuccessful()) {
                        Log.e(TAG, "订阅失败->" + activity.getPackageName());
                    }else{
                        Log.e(TAG,"订阅成功->" + activity.getPackageName());
                    }
                }
            });
        }
    }

    public void log(Activity activity, String event) {
        log(activity, event, null);
    }

    public void log(Activity activity, String event, Bundle bundle) {
        if (open()) {
            FirebaseAnalytics.getInstance(activity).logEvent(event, bundle == null ? new Bundle() : bundle);
            try {
                Log.e(TAG, "firebase:name:" + event + ",val->" + (bundle == null ? "" : bundle.toString()));
            }catch (Exception e){}
        }
    }
}
